package com.lz.library.adapter

import android.util.SparseArray
import android.view.View
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView

/**
 *  author: you : 2020/4/25
 */
class SimpleViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

    private val views = SparseArray<View>()

    var tag: Any? = null

    fun <T : View> getView(viewId: Int): T = (views[viewId] ?: itemView.findViewById<View>(viewId).also { views.put(viewId, it) }) as T

    fun setText(viewId: Int, strId: Int): SimpleViewHolder = also { getView<TextView>(viewId).setText(strId) }

    fun setText(viewId: Int, str: String) : SimpleViewHolder = also { getView<TextView>(viewId).text = str }

    fun setOnClickListener(viewId: Int, listener: View.OnClickListener): SimpleViewHolder = also { getView<View>(viewId).setOnClickListener(listener) }

    fun setOnLongClickListener(viewId: Int, listener: View.OnLongClickListener): SimpleViewHolder = also { getView<View>(viewId).setOnLongClickListener(listener) }
}